#!/bin/sh
# Utmconv.sh - Demo shell script for the program UTMCONV
# Author: Nils Maercklin, April 2007
#
# This demo script includes 4 examples for coordinate conversion with
# UTMCONV. Calculated coordinates are listed in the terminal window.
# Example 1: Convert lon/lat to UTM using defaults of UTMCONV
# Example 2: Convert lon/lat to UTM using a different reference
#            ellipsoid and output format
# Example 3: Convert UTM coordinates from Example 1 back to lon/lat.
# Example 4: General Transverse Mercator projection
#
# Relevant selfdoc: utmconv


### CREATE OR SELECT EXAMPLE COORDINATES:

# Input coordinate file for examples (longitude/latitude):
infile=lonlat.txt


# Create an ASCII file with some geographical coordinates:
# Column 1: Longitude in decimal degrees, negative W of zero-meridian
# Column 2: Latitude in decimal degrees, negative on southern hemisphere
if [ ! -f $infile ]; then
    echo "Creating coordinate file $infile ..."

    cat <<EOF > $infile
   -105.210911  39.746837 
     10.750000  59.933333 
    110.364444  -7.801389 
EOF
fi

# List input file:
echo "--------------------------------------------------------------"
echo "Longitudes and Latitudes ($infile):"
cat $infile



### EXAMPLE 1:
echo "--------------------------------------------------------------"
echo "Example 1: UTM Easting and Northing (utm1.txt):"

# Calculate UTM coordinates using the WGS 1984 ellipsoid 
# (default, idx=23):
# Output column 1: UTM Easting in meters
# Output column 2: UTM Northing in meters
# Output column 3: UTM Zone number, negative on southern hemisphere
utmconv < $infile verbose=1 > utm1.txt

cat utm1.txt

# Continue after pressing ENTER:
echo "...press ENTER to continue..." > /dev/stderr
read dummy



### EXAMPLE 2:
echo "--------------------------------------------------------------"
echo "Example 2: UTM Easting and Northing (utm2.txt):"

# Calculate UTM coordinates using the International 1924 ellipsoid, 
#  (idx=17) and the alphanumeric UTM Zone identifier:
# Output column 1: UTM Easting in meters
# Output column 2: UTM Northing in meters
# Output column 3: UTM Zone number and letter designator
utmconv < $infile idx=17 letter=1 verbose=1 > utm2.txt

cat utm2.txt

# Continue after pressing ENTER:
echo "...press ENTER to continue..." > /dev/stderr
read dummy



### EXAMPLE 3:
echo "--------------------------------------------------------------"
echo "Example 3: Lon and Lat from UTM coordinates (lonlat1.txt):"
echo "           left: true coordinates, right: converted"

# Convert the UTM coordinates from Example 1 back to longitude
# and latitude, setting output format to 6 decimal digits:
# Input format as the output of Example 1.
utmconv < utm1.txt invert=1 format=%.6f verbose=0 > lonlat1.txt

# List original input file and lonlat1.txt next to each other:
paste $infile lonlat1.txt

# Continue after pressing ENTER:
echo "...press ENTER to continue..." > /dev/stderr
read dummy



### EXAMPLE 4:

# Coordinates and central meridian:
lat=39.7468
lon=-105.2109
lon0=-105

echo "--------------------------------------------------------------"
echo "Example 4: general Transverse Mercator projection:"
echo "           lat=$lat lon=$lon lon0=$lon0"

# Transverse Mercator projection of a single lon/lat coordinate 
# pair on a user-specified ellipsoid, using central meridian lon0 
# and no false Easting/Northing:
# Note that with user-specified lon0, only the sign of the output 
# zone number (column 3) will be used in an inverse transformation.


# Do the Transverse Mercator projection:
echo $lon $lat | \
utmconv verbose=1 a=6378388 f=0.003 lon0=$lon0 xoff=0 ysoff=0


exit 0
